package com.restfinder.user.component;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.restfinder.common.component.utils.PropertiesUtils;
import com.restfinder.user.forms.RestaurantForm;

@Component
public class RestaurantValidator implements Validator {

	public boolean supports(Class<?> clazz) {
		return RestaurantForm.class.equals(clazz);
	}

	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmpty(errors, "restaurant.name", PropertiesUtils.getMessage("restaurant.name.empty"));	
		ValidationUtils.rejectIfEmpty(errors, "restaurant.description", PropertiesUtils.getMessage("restaurant.description.empty"));	
		ValidationUtils.rejectIfEmpty(errors, "restaurant.address", PropertiesUtils.getMessage("restaurant.address.empty"));	
		
		RestaurantForm rf = (RestaurantForm) target;
		if(rf.isNewComment()){
			ValidationUtils.rejectIfEmpty(errors, "comment.content", PropertiesUtils.getMessage("comment.content.empty"));	
		}
	}

}
